using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QTool.BEPUphysics;
using QTool.BEPUphysics.Entities.Prefabs;
using QTool.BEPUphysics.Entities;
using QTool.BEPUphysics.BroadPhaseEntries.MobileCollidables;
using QTool.BEPUphysics.BroadPhaseEntries;
using QTool.BEPUphysics.NarrowPhaseSystems.Pairs;
using QTool.NetFrame;
using QTool.BEPUphysics.Materials;
using QTool.BEPUphysics.CollisionRuleManagement;
using QTool.BEPUphysics.CollisionShapes.ConvexShapes;
using QTool.BEPUphysics.CollisionShapes;
using QTool.BEPUphysics.BroadPhaseSystems;

namespace QTool.NetFrame
{

    [DisallowMultipleComponent]
    public abstract class FixCollider : QFixBehaviour
    {
        [QName("触发器")]
        public bool isTrigger;
        public CollisionRule Rule => isTrigger ? CollisionRule.NoSolver : CollisionRule.Normal;
        public EntityShape Shape
        {
            get
            {
                if (!Application.isPlaying) return CreateShape();
                if (_shape == null)
                {
                    var shap=CreateShape();
                    _shape = shap;
                    _shape.Tag = this;
                }
                return _shape;
            }
        }
        EntityShape _shape;
    
        public override void OnFixAwake()
        {
            base.OnFixAwake();
            if (GetComponentInParent<FixRigidbody>() == null)
            {
                var lastEntity = NetEntity.fixEntity;
                NetEntity.fixEntity = new Entity(Shape);
                NetEntity.spaceObject = NetEntity.fixEntity;
                if (NetEntity is IBroadPhaseEntryOwner entry)
                {
                    entry.Entry.Tag = this;
                }
                NetEntity.fixEntity.Tag = this;
                NetEntity.Position = lastEntity.Position;
                NetEntity.Rotation = lastEntity.Rotation;
                NetEntity.fixEntity.CollisionInformation.CollisionRules.personal = Rule;
            }
        }
        protected abstract EntityShape CreateShape();
        protected virtual void OnDrawGizmosSelected()
        {
            Gizmos.color =Color.Lerp( Color.blue,Color.clear,0.5f);
            Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
            if (Shape is BoxShape box)
            {
                Gizmos.DrawWireCube(Vector3.zero, new Vector3((float)box.Width, (float)box.Height, (float)box.Length));
            }
            else if(Shape is SphereShape sphere)
            {
                Gizmos.DrawWireSphere(Vector3.zero, (float)sphere.Radius);
            }
            else if(Shape is CapsuleShape shape)
            {
                Gizmos.DrawWireSphere(Vector3.up*(float)shape.Length/2, (float)shape.Radius);
                Gizmos.DrawLine(Vector3.right * shape.Radius.Float + Vector3.down *shape.Length.Float / 2, Vector3.right * shape.Radius.Float+ Vector3.up * shape.Length.Float / 2);
                Gizmos.DrawLine(-Vector3.right * shape.Radius.Float + Vector3.down *shape.Length.Float / 2,- Vector3.right * shape.Radius.Float + Vector3.up * shape.Length.Float / 2);
                Gizmos.DrawLine(Vector3.forward * shape.Radius.Float  + Vector3.down * shape.Length.Float / 2, Vector3.forward * shape.Radius.Float  + Vector3.up * shape.Length.Float / 2);
                Gizmos.DrawLine(-Vector3.forward * shape.Radius.Float + Vector3.down * shape.Length.Float / 2, -Vector3.forward * shape.Radius.Float + Vector3.up * shape.Length.Float / 2);
                Gizmos.DrawWireSphere(Vector3.down * (float)shape.Length / 2, (float)shape.Radius);
            }
          
        }
    }

}
